//
//  AppleScriptController.m
//  Do Not Disturb
//
//  Created by Chris Karr on 11/9/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "AppleScriptController.h"
#import "DNDDefines.h"

@implementation AppleScriptController

- (void) awakeFromNib
{
	[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(handleUpdate:) name:DND_PREDICTION_UPDATE object:nil];
}

- (void) handleUpdate:(NSNotification *) msg
{
	[[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:DND_PREDICTION_UPDATE object:nil];
	
	NSString * prediction = [predictor getPredictedStatus];
	
	if (prediction == nil)
		prediction = @"Unknown";

	NSArray * paths = [[NSBundle mainBundle] pathsForResourcesOfType:@"scpt" inDirectory:@"AppleScript"];
		
	NSEnumerator * iter = [paths objectEnumerator];
	NSString * path = nil;

	while ((path = [iter nextObject]) != nil)
	{
		NSArray * args = [NSArray arrayWithObjects:path, prediction, nil];
		NSTask * task = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/osascript" arguments:args];
		
		[task waitUntilExit];
	}

	[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(handleUpdate:) name:DND_PREDICTION_UPDATE object:nil];
}

@end
